package com.emuporium.gymrat.android.exercises;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.emuporium.gymrat.android.R;
import com.emuporium.gymrat.android.database.DatabaseExerciseAdapter;

public class ExercisesHome extends ListActivity {
	private static final int INFO_ID = Menu.FIRST;
	private static final int ADD_TO_ROUTINE_ID = Menu.FIRST + 1;
	DatabaseExerciseAdapter db;
	CursorAdapter adapter;
	static int currentExercise = 0;
	Cursor c;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.exercises_home);
		View footer = getLayoutInflater().inflate(
				R.layout.exercise_home_list_footer, null);
		getListView().addFooterView(footer);
	}

	@Override
	public void onResume() {
		super.onResume();

		db = new DatabaseExerciseAdapter(this);
		db.open();
		// Get all exercises
		c = db.getAll();
		startManagingCursor(c);

		

		adapter = new SimpleCursorAdapter(this,
				R.layout.exercises_home_list_item, c, new String[] { "name",
						"musclegroup" }, new int[] {
						R.id.exercises_home_list_name,
						R.id.exercises_home_list_group });

		setListAdapter(adapter);
		getListView().setSelection(currentExercise);
		registerForContextMenu(getListView());

		Button addExercise = (Button) findViewById(R.id.exercise_home_list_add_exercise);
		addExercise.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				addExercise();
			}
		});

	}

	@Override
	public void onPause() {
		super.onPause();
//		c.close();
		db.close();
//		stopManagingCursor(c);
		
	}

	private void addExercise() {
		// Intent intent = new Intent(ExercisesHome.this, ProgramsHome.class);
		// startActivity(intent);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);

		menu.add(0, INFO_ID, Menu.NONE, "Details");
		menu.add(0, ADD_TO_ROUTINE_ID, Menu.NONE, "Add to Routine...");
		// Set the title of the context menu to the name of the exercise that
		// created the menu.
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
		CharSequence title = ((TextView) info.targetView
				.findViewById(R.id.exercises_home_list_name)).getText();
		menu.setHeaderTitle(title);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		super.onContextItemSelected(item);
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
				.getMenuInfo();

		switch (item.getItemId()) {
		case INFO_ID:
			Intent intent = new Intent(ExercisesHome.this,
					ExercisesDetails.class);
			c.moveToPosition(menuInfo.position);
			currentExercise = menuInfo.position;
			int id = c.getInt(c.getColumnIndex("_id"));
			Toast.makeText(this, "Get info for id:" + id, Toast.LENGTH_SHORT)
					.show();
			intent.putExtra("gymrat.id", id);
			startActivity(intent);
			break;
		case ADD_TO_ROUTINE_ID:
			Toast.makeText(this, "Add this exercise to a Routine",
					Toast.LENGTH_SHORT).show();
			break;
		}

		return true;
	}

	@Override
	protected void onListItemClick(ListView list, View v, int pos, long row) {
		c.moveToPosition(pos);
		int id = c.getInt(c.getColumnIndex("_id"));
		currentExercise = pos;
		// TODO: check intent, if this exercise list was opened from an "add to
		// routine" prompt, return there, else default to click = info
		Intent intent = new Intent(ExercisesHome.this, ExercisesDetails.class);
		intent.putExtra("gymrat.id", id);
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		new MenuInflater(this).inflate(R.menu.exercises_home, menu);
		return (super.onCreateOptionsMenu(menu));
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.exercises_home_menu_add_exercise:
			addExercise();
			break;
		}

		return super.onOptionsItemSelected(item);
	}

}
